window: Be smarter about computing the default size
authorBenjamin Otte <otte@redhat.com>
Tue, 26 Apr 2011 22:05:01 +0000 (00:05 +0200)
committerBenjamin Otte <otte@redhat.com>
Tue, 26 Apr 2011 22:27:29 +0000 (00:27 +0200)
See the code comments for the reasoning behind this. After we don't
force a "guessed" minimum size for labels anymore, a lot of issues
started to surface that this patch attempts to fix. In particular:

1) Tooltips where wrapped as much as possible.
2) The recentchooser submenu displayed only ellipsize dots.

gtk/gtkwindow.c

index 4042d8246d4119522ffb930a028bce73492bed62..3ac8628bd5af2a812a1d3ec4bbcac9fe028d895a 100644 (file)
@@ -4833,18 +4833,71 @@ gtk_window_unmap (GtkWidget *widget)
     gtk_widget_unmap (child);
 }
 
+/* (Note: Replace "size" with "width" or "height". Also, the request
+ * mode is honoured.)
+ * For selecting the default window size, the following conditions
+ * should hold (in order of importance):
+ * - the size is not below the minimum size
+ *   Windows cannot be resized below their minimum size, so we must
+ *   ensure we don't do that either.
+ * - the size is not above the natural size
+ *   It seems weird to allocate more than this in an initial guess.
+ * - the size does not exceed that of a maximized window
+ *   We want to see the whole window after all.
+ *   (Note that this may not be possible to achieve due to imperfect
+ *    information from the windowing system.)
+ */
+
+/* We use these for now to not make windows too big by accident. Note
+ * that we still clamp these numbers by screen size. Also note that
+ * minimum size still overrides this. So keep your windows small! :)
+ */
+#define MAX_DEFAULT_WINDOW_WIDTH 640
+#define MAX_DEFAULT_WINDOW_HEIGHT 480
+
 static void
 gtk_window_guess_default_size (GtkWindow *window,
                                gint      *width,
                                gint      *height)
 {
   GtkWidget *widget = GTK_WIDGET (window);
-  GtkRequisition requisition;
+  GdkScreen *screen;
+  int minimum, natural;
+
+  screen = gtk_widget_get_screen (widget);
+
+  *width = gdk_screen_get_width (screen);
+  *height = gdk_screen_get_height (screen);
+
+  if (*width < *height)
+    {
+      /* landscape */
+      *width = MIN (*width, MAX_DEFAULT_WINDOW_WIDTH);
+      *height = MIN (*height, MAX_DEFAULT_WINDOW_HEIGHT);
+    }
+  else
+    {
+      /* portrait */
+      *width = MIN (*width, MAX_DEFAULT_WINDOW_HEIGHT);
+      *height = MIN (*height, MAX_DEFAULT_WINDOW_WIDTH);
+    }
 
-  gtk_widget_get_preferred_size (widget, &requisition, NULL);
+  if (gtk_widget_get_request_mode (widget) == GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT)
+    {
+      gtk_widget_get_preferred_height (widget, &minimum, &natural);
+      *height = MAX (minimum, MIN (*height, natural));
 
-  *width = requisition.width;
-  *height = requisition.height;
+      gtk_widget_get_preferred_width_for_height (widget, *height, &minimum, &natural);
+      *width = MAX (minimum, MIN (*width, natural));
+    }
+  else /* GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH or CONSTANT_SIZE */
+    {
+      gtk_widget_get_preferred_width (widget, &minimum, &natural);
+      *width = MAX (minimum, MIN (*width, natural));
+
+      gtk_widget_get_preferred_height_for_width (widget, *width, &minimum, &natural);
+      *height = MAX (minimum, MIN (*height, natural));
+    }
 }
 
 static void